<?php

/**
 * GalleryController is the default controller for this application
 */
class GalleryController extends CustomControllerBase {
	
	public $systemCategoryDataService = null;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'user_gallery' );
		$this->setModuleName ( "Gallery" );
		
		//Check if Blogging Module is enabled
		if ($this->currentPackageOption) {
			$module = MODULE_GALLERY;
			$isEnabled = $this->currentPackageOption->isEnabled ( $module );
			if ($isEnabled == false) {
				$url = $this->getApplicationUrl ();
				CommonUtils::redirect ( $url );
				return - 1;
			}
		}
		
		// Initialise userPhotoDataService		
		$this->systemCategoryDataService = new SystemCategoryDataService ();
		
		// Get list category
		$arrElement = array ();
		$arrElement ['ownerId'] = $this->ownerId;
		$arrElement ['type'] = CATEGORY_PHOTO;
		
		$catList = $this->systemCategoryDataService->getItems ( $arrElement );
		
		$this->smarty->assign ( 'catList', $catList );
		$this->smarty->assign ( 'catListCount', count ( $catList ) );
	
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		// Get 5 latest Blog entry
		$arrElement = array ();
		$arrElement ['status'] = UserBlogEntity::ACTIVE;
		$arrElement ['ownerId'] = $this->ownerId;
		$cat = $this->getQuery ( 'cat' );
		if ($cat != "") {
			$arrElement ['categoryId'] = $cat;
		}
		
		$numRecordPerPage = ITEM_PER_PAGE;
		
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get total records of product options based on search criterias
		$totals = $this->userPhotoDataService->getTotals ( $arrElement, " id DESC" );
		
		$itemsList = $this->userPhotoDataService->getItems ( $arrElement, " id DESC", $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		//$link = Zend_Registry::get('config')->APPLICATION_URL .'/'. $this->ownerName ."/Gallery/Index";
		$link = $this->getApplicationUrl () . "Gallery/Index";
		$link .= $cat != '' ? "/cat/{$cat}" : '';
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 2, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		if (count ( $itemsList ) > 0) {
			foreach ( $itemsList as $item ) {
				$item->description = htmlspecialchars_decode ( $item->description, ENT_QUOTES );
			}
		}
		
		$this->smarty->assign ( 'photoItemList', $itemsList );
		$this->smarty->assign ( 'pageLink', $pageLink );
		
		$this->smarty->assign ( 'numberPhotoItemList', count ( $itemsList ) );
		
		if ($this->isMobileMode == false) {
			// Assign Smarty Content
			$this->assignSmartyContent ( 'Gallery/index.tpl' );
		} else {
			// Assign Smarty Content
			$this->assignSmartyMobileContent ( 'Gallery/m_index.tpl' );
		}
	}
}
